package com.welsy.stats.command
{
	import com.welsy.stats.model.StatsModelLocator;
	import com.welsy.stats.vo.IterClassVO;
	import com.adobe.cairngorm.commands.ICommand;
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.control.CairngormEvent;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import com.welsy.util.NamespaceLocator;
	import com.welsy.stats.business.GetPublishCoursesDelegate;
	import com.welsy.stats.event.GetPublishCoursesEvent;
	import com.welsy.qnames.DataTypes;
	import com.welsy.authoring.vo.CourseVO;
	
	public class GetPublishCoursesCommand implements ICommand, IResponder
	{
		[Bindable] private var smodel : StatsModelLocator = StatsModelLocator.getInstance();	
		
		public function execute(event : CairngormEvent):void {
			var delegate : GetPublishCoursesDelegate = new GetPublishCoursesDelegate(this);
			var getPublishCourses : GetPublishCoursesEvent = GetPublishCoursesEvent(event);
			
			delegate.getPublishCourses(getPublishCourses.authorId);
		}
		
		public function result(event : Object) :void {
			//parse the server result
			var xres : XMLList = XMLList(event.result);
			
			var courses : ArrayCollection = new ArrayCollection;
			for each(var ct : XML in xres.descendants(DataTypes.TAG_COURSES).children()) {
				//CourseType
				var courseVO : CourseVO = new CourseVO;
				courseVO.id = ct.child(DataTypes.TAG_COURSEID);
				courseVO.parentCourseId = ct.child(DataTypes.TAG_PARENTCOURSEID);
				courseVO.title = ct.child(DataTypes.TAG_TITLE);
				courseVO.description = ct.child(DataTypes.TAG_DESCRIPTION);
				courseVO.credits = ct.child(DataTypes.TAG_CREDITS);
				courseVO.webLink = ct.child(DataTypes.TAG_WEBLINK);
				courseVO.notes = ct.child(DataTypes.TAG_NOTE);
				courseVO.state = ct.child(DataTypes.TAG_STATE);
				courseVO.authorId = ct.child(DataTypes.TAG_AUTHORID);
				courseVO.mapView = ct.child(DataTypes.TAG_MAPVIEW);
				courseVO.navigationMap = ct.child(DataTypes.TAG_NAVIGATIONMAP);
				
				courses.addItem(courseVO);
			}
			
			smodel.courses = courses;
		}
		
		public function fault( event : Object ):void {
			Alert.show("GetPublishCoursesCommand.fault()!");
		}
		
	}
}